﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
using T4UtilityBelt.DataDesigns;

namespace T4UtilityBelt.LightSwitch.FileModels
{
    public partial class ModelFragment
    {
        public T4UtilityBelt.DataDesigns.EntityModel ToEntityModel()
        {
            T4UtilityBelt.DataDesigns.EntityModel model = new T4UtilityBelt.DataDesigns.EntityModel();

            Dictionary<string, Association> associations = new Dictionary<string, Association>();
            Dictionary<string, EntitySet> entitySets = new Dictionary<string, EntitySet>();
            Dictionary<string, Entity> entities = new Dictionary<string, Entity>();

            foreach (Association association in Association)
            {
                if (!associations.ContainsKey(association.Name))
                {
                    associations.Add(association.Name, association);
                }
            }

            foreach (EntityContainer entityContainer in EntityContainer)
            {
                foreach (EntitySet entitySet in entityContainer.EntitySet)
                {
                    if (!entitySets.ContainsKey(entitySet.Name))
                    {
                        entitySets.Add(entitySet.EntityType, entitySet);
                    }
                }
            }

            foreach (EntityType entityType in this.EntityTypes)
            {
                Entity entity = new Entity();
                entity.Name = entityType.Name;

                if (entitySets.ContainsKey(entityType.Name))
                {
                    EntitySet entitySet = entitySets[entityType.Name];
                    entity.PluralName = entitySet.Name;
                }

                model.Entities.Add(entity);
                entities.Add(entity.Name, entity);
            }

            foreach (EntityType entityType in this.EntityTypes)
            {
                Entity entity = entities[entityType.Name];

                foreach (EntityTypeProperties properties in entityType.EntityTypeProperties)
                {
                    if (properties.KeyProperty != null)
                    {
                        foreach (KeyProperty property in properties.KeyProperty)
                        {
                            T4UtilityBelt.DataDesigns.KeyProperty kp = new T4UtilityBelt.DataDesigns.KeyProperty { Entity = entity, Name = property.Name, PropertyType = property.PropertyType };
                            if (property.KeyPropertyAttributes != null
                                && property.KeyPropertyAttributes.Length > 0)
                            {
                               // KeyPropertyAttributes kpa = property.KeyPropertyAttributes[0];
                                kp.PropertyType = property.PropertyType;
                                //kp.NotSearchable = kpa.NotSearchable != null;
                                //kp.Required = kpa.Required != null;
                            }
                            entity.Properties.Add(kp);
                        }
                    }

                    //if (properties.EntityProperty != null)
                    //{
                    //    foreach (EntityProperty property in properties.EntityProperty)
                    //    {
                    //        T4UtilityBelt.EntityModel.Property kp = new Property { Entity = entity, Name = property.Name, PropertyType = property.PropertyType };
                    //        if (property.EntityPropertyAttributes != null
                    //            && property.EntityPropertyAttributes.Length > 0)
                    //        {
                    //            EntityPropertyAttributes kpa = property.EntityPropertyAttributes[0];

                    //            kp.Required = kpa.Required != null;

                    //            if (kpa.MaxLength != null)
                    //            {
                    //                if (kpa.MaxLength.Length >= 1)
                    //                {
                    //                    int l;
                    //                    if (int.TryParse(kpa.MaxLength[0].Value, out l))
                    //                        kp.MaxLength = l;
                    //                }
                    //                if (kpa.MaxLength.Length >= 2)
                    //                {
                    //                    int l;
                    //                    if (int.TryParse(kpa.MaxLength[1].Value, out l))
                    //                        kp.MaxDecimals = l;
                    //                }
                    //            }
                    //        }
                    //        entity.Properties.Add(kp);
                    //    }
                    //}

                    //if (properties.NavigationProperty != null)
                    //{
                    //    foreach (NavigationProperty property in properties.NavigationProperty)
                    //    {
                    //        T4UtilityBelt.EntityModel.NavigationProperty2 kp =
                    //            new EntityModel.NavigationProperty2
                    //                {
                    //                    Name = property.Name,
                    //                    Association = property.Association,
                    //                    FromEnd = property.FromEnd
                    //                };
                    //        kp.PropertyType = ":Int32";
                    //        kp.FromEndEntity = entities[kp.FromEnd];
                    //        kp.ToEnd = property.ToEnd;
                    //        kp.ToEndEntity = entities[kp.ToEnd];
                    //        if (associations.ContainsKey(property.Association))
                    //        {
                    //            kp.Multiplicity = GetMultiplicity(associations[property.Association], entity);
                    //        }
                    //        entity.NavigationProperties2.Add(kp);
                    //    }
                    //}
                }
            }

            return model;
        }

        private static Multiplicity GetMultiplicity(Association association, Entity entity)
        {
            if (association != null)
            {
                foreach (AssociationEnd associationEnd in association.AssociationEnd)
                {
                    if (!string.IsNullOrEmpty(associationEnd.Multiplicity)
                        && associationEnd.EntityType == entity.Name)
                    {
                        return (Multiplicity)Enum.Parse(typeof(Multiplicity), associationEnd.Multiplicity);
                    }
                }
            }
            return Multiplicity.None;
        }
    }
}